package com.syezon.wifikey.ad;

import android.arch.lifecycle.MutableLiveData;
import android.arch.lifecycle.ViewModel;

import com.blankj.utilcode.util.AppUtils;
import com.hodanet.appadvplatclient.AdvClient;
import com.hodanet.appadvplatclient.ad.AdInfo;
import com.hodanet.appadvplatclient.api.BaseResult;
import com.syezon.wifikey.ad.config.ConfigObserver;
import com.syezon.wifikey.channel.ChannelConfig;
import com.syezon.wifikey.redbag.info.BoxInfo;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.BiFunction;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Administrator on 2019/1/16.
 */

public class BoxAdViewModel extends ViewModel implements ConfigObserver {

    public static final String KEY_PREFIX = "AD_VIEW_MODEL_";

    protected MutableLiveData<List<BoxInfo>> adLiveData = new MutableLiveData<>();

    private static List<BoxInfo> EMPTY = new ArrayList<>();


    public BoxAdViewModel() {
        ConfigService.getInstance().registerObserver(this);
    }

    public MutableLiveData<List<BoxInfo>> getAdLiveData() {
        return adLiveData;
    }

    @Override
    public void onChange(boolean showAd) {
        if (showAd) {
            getData();
        } else {
            adLiveData.postValue(EMPTY);
        }
    }

    @Override
    protected void onCleared() {
        super.onCleared();
        ConfigService.getInstance().unRegisterObserver(this);
    }

    protected void getData() {
        Observable<BaseResult<List<AdInfo>>> ad1 = Observable.create(new ObservableOnSubscribe<BaseResult<List<AdInfo>>>() {
            @Override
            public void subscribe(ObservableEmitter<BaseResult<List<AdInfo>>> e) throws Exception {
                BaseResult<List<AdInfo>> advs = AdvClient.getInstance().getAdvs(AdPositionEnum.BOX_AD_1.getPosId(), ChannelConfig.getChannel(), AppUtils.getAppVersionCode());
                e.onNext(advs);
            }
        });
        Observable<BaseResult<List<AdInfo>>> ad2 = Observable.create(new ObservableOnSubscribe<BaseResult<List<AdInfo>>>() {
            @Override
            public void subscribe(ObservableEmitter<BaseResult<List<AdInfo>>> e) throws Exception {
                BaseResult<List<AdInfo>> advs = AdvClient.getInstance().getAdvs(AdPositionEnum.BOX_AD_2.getPosId(), ChannelConfig.getChannel(), AppUtils.getAppVersionCode());
                e.onNext(advs);
            }
        });
        Observable.zip(ad1, ad2, new BiFunction<BaseResult<List<AdInfo>>, BaseResult<List<AdInfo>>, List<BoxInfo>>() {
            @Override
            public List<BoxInfo> apply(BaseResult<List<AdInfo>> listBaseResult, BaseResult<List<AdInfo>> listBaseResult2) throws Exception {
                List<BoxInfo> result = new ArrayList<>();
                String title = "会员福利";
                String title2 = "休闲娱乐";
                List<AdInfo> data1 = listBaseResult.getData();
                List<AdInfo> data2 = listBaseResult2.getData();

                BoxInfo boxInfoTitle = new BoxInfo();
                boxInfoTitle.setType(BoxInfo.TYPE_TITLE);
                boxInfoTitle.setTitle(title);
                result.add(boxInfoTitle);
                for (int j = 0; data1 != null && j < data1.size(); j++) {
                    AdInfo adInfo = data1.get(j);
                    BoxInfo boxInfoItem = new BoxInfo();
                    boxInfoItem.setType(BoxInfo.TYPE_AD_TOOLS);
                    boxInfoItem.setAdInfo(adInfo);
                    result.add(boxInfoItem);
                }
                BoxInfo boxInfo = new BoxInfo();
                boxInfo.setType(BoxInfo.TYPE_DIVIDER);
                result.add(boxInfo);


                BoxInfo boxInfoTitle2 = new BoxInfo();
                boxInfoTitle2.setType(BoxInfo.TYPE_TITLE);
                boxInfoTitle2.setTitle(title2);
                result.add(boxInfoTitle2);
                for (int j = 0; data2 != null && j < data2.size(); j++) {
                    AdInfo adInfo = data2.get(j);
                    BoxInfo boxInfoItem = new BoxInfo();
                    boxInfoItem.setType(BoxInfo.TYPE_AD_PIC_TEXT);
                    boxInfoItem.setAdInfo(adInfo);
                    result.add(boxInfoItem);
                }
                return result;
            }
        }).subscribeOn(Schedulers.io())
                .subscribe(new Observer<List<BoxInfo>>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(List<BoxInfo> boxInfos) {
                        adLiveData.postValue(boxInfos);
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }
}
